package jjn.round1;

/**
 * @author Jjn
 * @since 2022-08-15 16:32
 */
public class SortingAlgorithm_SelectionSort implements SortAlgorithm {
    
    @Override
    public void sort(int... nums) {
        if (nums == null || nums.length <= 1) {
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            int minIndex = i;
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[j] < num) {
                    num = nums[j];
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                swap(minIndex, i, nums);
            }
        }
    }
    
    private void swap(int i, int j, int... nums) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
    
    @Override
    public String getName() {
        return "Selection sort";
    }
}
